package com.mlink.vs.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.seasar.struts.annotation.Execute;

import com.google.gson.Gson;
import com.mlink.vs.entity.PiccUser;
import com.mlink.vs.service.DbTimeService;
import com.mlink.vs.service.PiccMsgqueueService;
import com.mlink.vs.service.PiccMsgsendLogService;
import com.mlink.vs.service.PiccUserService;
import com.mlink.vs.util.Constants;
import com.mlink.vs.util.StreamUtil;

public class CaocaoAction {
	/**
	 * request请求对象
	 */
	@Resource
	private HttpServletRequest req;

	/**
	 * response请求对象
	 */
	@Resource
	private HttpServletResponse rep;
	
	@Resource
	DbTimeService dbTimeService;
	
	@Resource
	PiccUserService piccUserService;
	
	@Resource
	PiccMsgqueueService piccMsgqueueService;
	
	@Resource
	PiccMsgsendLogService piccMsgsendLogService;
	
	//坐席端推送接口
	@Execute(validator = false)
	public String userListGet() {
		Gson gson = new Gson();
		StringBuffer indata = StreamUtil.getRequestBody(req);
		Map stsMap = new HashMap<String, String>();

		Map<String, String> mapData = gson.fromJson(indata.toString(), Map.class);
		if (mapData == null)
			return null;
		
		String seatUserId = (String) mapData.get("seatUserId");
		String token = (String) mapData.get("token");

		try {
			rep.setCharacterEncoding("utf-8");
			if (StringUtils.isBlank(seatUserId)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "seatUserId不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			if (StringUtils.isBlank(token)) {
				stsMap.put("sts", "2");
				stsMap.put("msg", "token不能为空");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
				return null;
			}
			
			PiccUser piccUser = piccUserService.findById(seatUserId);
			if(piccUser!=null){
				String userType=Constants.USER_TYPE_FRONT;
				String groupName="曹操专车";
				List<PiccUser> piccUserList = piccUserService.findByGroupName(groupName, userType);
				if(piccUserList!=null && piccUserList.size()>0){
					List list = new ArrayList();
					for (int i = 0; i < piccUserList.size(); i++) {
						Map<String,String> a = new HashMap<String,String>();
						a.put("userId", piccUserList.get(i).userId);
						a.put("userName", piccUserList.get(i).userName);
						a.put("userContact", piccUserList.get(i).userContact==null ? " ":piccUserList.get(i).userContact);
						a.put("userCompany", piccUserList.get(i).userCompany==null ? " ":piccUserList.get(i).userCompany);
						list.add(a);
					}
					stsMap.put("sts", "0");
					stsMap.put("list", list);
					rep.getWriter().println(gson.toJson(stsMap));
					rep.getWriter().flush();
				}else{
					stsMap.put("sts", "2");
					rep.getWriter().println(gson.toJson(stsMap));
					rep.getWriter().flush();
				}
			}else{
				stsMap.put("sts", "2");
				rep.getWriter().println(gson.toJson(stsMap));
				rep.getWriter().flush();
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}
}
